<?
/************************************************************************/
/* 	The author of this program code:                                    */
/*  Dmitry Sheiko (sheiko@cmsdevelopment.com)	                    	*/
/* 	Copyright by Dmitry Sheiko											*/
/* 	http://www.cmsdevelopment.com     									*/
/************************************************************************/


/**
	* @return void
	* @param void
	* @desc Manage survey 
	**/

function get_survey(){
	global $http_path, $root_path, $env, $root_path, $sapi_obj;
	$_POST["data_file"] = "survey1.xml";
	$full_datafilename = $root_path."usr/xml/vdb/".$_POST["data_file"];
	$vdb = new sapi_vdb($full_datafilename);
	$vdb->save_data_row(false, true);
	$stream=array();
	for ($i=1; $i<5; $i++){
		$stream["0"]["PER_RESULT".$i] = 0;
		$stream["0"]["LENGHT_RESULT".$i] = 5;
	}

	if(file_exists($full_datafilename)) {
		$objdata = $vdb->get_data($full_datafilename);
		$sapi_obj->env["survey_display_results"]=0;
		if($objdata) {
			foreach($objdata as $fetch) {
				if(!isset($stream["0"]["RESULT".$fetch["RESULT"]])) $stream["0"]["RESULT".$fetch["RESULT"]]=0;
				$stream["0"]["RESULT".$fetch["RESULT"]]++;
				if(!isset($sum)) $sum=0;
				$sum++;
				if ($fetch["IP"]==$_SERVER["REMOTE_ADDR"]) $sapi_obj->env["survey_display_results"]=1;
			}
			foreach($stream["0"] as $key => $fetch) {
				if($sum) {
					$stream["0"]["PER_".$key] = sprintf("%.1f", 100/$sum * $fetch);
					$stream["0"]["LENGHT_".$key] = (90/$sum * $fetch); //Adjust for desired line lenght
				}
				else{
					$stream["0"]["PER_".$key] = " 0";
					$stream["0"]["LENGHT_".$key] = 5;
				}
			}

		}
	}
	
	if($stream) {
		$__counter = 0;
		$sapi_obj->env["this.lenght"] = count($stream);
		foreach ($stream as $key => $fetch) {
			$stream[$key]["counter"] = ++$__counter;
		}
	}
	

	return $stream;
}

?>